package com.thinkingstar.iads.datacenter.serviceImpl;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.thinkingstar.iads.common.utils.FileCommonUtil;
import com.thinkingstar.iads.common.utils.InitSystemProperties;
import com.thinkingstar.iads.common.utils.POIFileUtil;
import com.thinkingstar.iads.cs.sys.enumerate.WIDGET_TYPE;
import com.thinkingstar.iads.datacenter.entity.DcManufacturerEquipmentField;
import com.thinkingstar.iads.datacenter.entity.dao.DaoDcProjectEquipmentField;
import com.thinkingstar.iads.projectcenter.entity.PcProjectFile;
import com.thinkingstar.iads.projectcenter.entity.dao.DaoPcProjectFile;
import com.thinkingstar.iads.projectcenter.serviceImpl.PreviewFileServiceImpl;

@IocBean
public class EquipmentServiceImpl {
	
	@Inject
	private DaoDcProjectEquipmentField daoDcProjectEquipmentField;
	
	@Inject
	private PreviewFileServiceImpl previewFileServiceImpl;
	
	@Inject
	private DaoPcProjectFile daoPcProjectFile;
	
	private final Document getDocument(){
		String html = InitSystemProperties.getHtmlTemplate();
		Document document = Jsoup.parseBodyFragment(html);
		return document;
	}
	
/*	private String getUrlById(String file_id){
        PcProjectFile pcProjectFile = daoPcProjectFile.fetch(file_id);
        if(pcProjectFile != null){
            String relative_path = pcProjectFile.getRelativePaths();
            String open_url = previewServer+"/"+FileCommonUtil.pathToURI(relative_path);
            String url = previewServer+"/onlinePreview?url="+open_url;
            return url;
        }
		return "";
	}*/
	
	//获取设备资料
	public String getEquipmentBasicById(String id){
		DcManufacturerEquipmentField equipment_field = daoDcProjectEquipmentField.findObjByManfacturerEquipmentId(id);
		if(equipment_field != null){
			return equipment_field.getEquipment_basics();
		}
		return "";
	}
	
	//获取设备厂商信息
	public String getEquipmentCsxxById(String id){
		DcManufacturerEquipmentField equipment_field = daoDcProjectEquipmentField.findObjByManfacturerEquipmentId(id);
		if(equipment_field != null){
			return equipment_field.getEquipment_csxx();
		}
		return "";
	}
	
	//获取设备照片
	public String getEquipmentPhotoById(String id){
		DcManufacturerEquipmentField equipment_field = daoDcProjectEquipmentField.findObjByManfacturerEquipmentId(id);
		if(equipment_field != null){
			String equipment_photo_id = equipment_field.getEquipment_photo();
			if(StringUtils.isNotBlank(equipment_photo_id)){
				String [] photo_ids = equipment_photo_id.split(",");
				return previewFileServiceImpl.getUrls(photo_ids);
			}
		}
		return "";
	}
	
	//获取设备土建条件图
	public String getEquipmentTjtjtById(String id){
		DcManufacturerEquipmentField equipment_field = daoDcProjectEquipmentField.findObjByManfacturerEquipmentId(id);
		if(equipment_field != null){
			String equipment_tjtjt_id = equipment_field.getEquipment_tjtjt();
			if(StringUtils.isNotBlank(equipment_tjtjt_id)){
				String [] tjtjt_ids = equipment_tjtjt_id.split(",");
				return previewFileServiceImpl.getUrlByFiles(tjtjt_ids);
			}
		}
		return "";
	}
	
	//获取土建条件图页
	public String getTjtjtHtmlById(String id , HttpServletRequest request){
		DcManufacturerEquipmentField equipment_field = daoDcProjectEquipmentField.findObjByManfacturerEquipmentId(id);
		if(equipment_field != null){
			String equipment_tjtjt_id = equipment_field.getEquipment_tjtjt();
			if(StringUtils.isNotBlank(equipment_tjtjt_id)){
				String [] tjtjt_ids = equipment_tjtjt_id.split(",");
				//整理
				Element content_element = getDocument().getElementsByAttributeValue("data_id","content").first();
				StringBuilder result_html = new StringBuilder();
				for(String tjtjt_id : tjtjt_ids){
					PcProjectFile pcProjectFile = daoPcProjectFile.fetch(tjtjt_id);
					Element btn_element = getDocument().getElementsByAttributeValue("data_id", "btn").first();
					String file_id = pcProjectFile.getId();
					Element _element = getDocument().getElementsByAttributeValue("data_id", WIDGET_TYPE.DWG.getCode()).first();
					_element.child(1).attr("data_id", file_id).attr("src", previewFileServiceImpl.getUrl(file_id,request));
					btn_element.child(0).text(pcProjectFile.getName());
					btn_element.child(1).remove();
					btn_element.child(1).attr("download_path", file_id).text("下载文件"); //文件id
					String excel_html = btn_element.toString() + _element.toString();
					result_html.append(excel_html);
				}
				content_element.append(result_html.toString());
				return content_element.toString();
			}
		}
		return "";
	}
	
	
	//获取设备用户需求书
	public String getEquipmentYhxqsById(String id){
		DcManufacturerEquipmentField equipment_field = daoDcProjectEquipmentField.findObjByManfacturerEquipmentId(id);
		if(equipment_field != null){
			String equipment_yhxqs_id = equipment_field.getEquipment_yhxqs();
			if(StringUtils.isNotBlank(equipment_yhxqs_id)){
				String [] yhxqs_ids = equipment_yhxqs_id.split(",");
				return previewFileServiceImpl.getUrlByFiles(yhxqs_ids);
			}
		}
		return "";
	}
	
	//获取设备技术接口
	public String getEquipmentJsjkById(String id){
		DcManufacturerEquipmentField equipment_field = daoDcProjectEquipmentField.findObjByManfacturerEquipmentId(id);
		if(equipment_field != null){
			String equipment_jsjk_id = equipment_field.getEquipment_jsjk();
			if(StringUtils.isNotBlank(equipment_jsjk_id)){
				String [] jsjk_ids = equipment_jsjk_id.split(",");
				return previewFileServiceImpl.getUrlByFiles(jsjk_ids);
			}
		}
		return "";
	}
	
	//获取用户群bim模型
	public String getEquipmentBimodel(String id){
		DcManufacturerEquipmentField equipment_field = daoDcProjectEquipmentField.findObjByManfacturerEquipmentId(id);
		if(equipment_field != null){
			String equipment_bimodel_id = equipment_field.getEquipment_bim_model();
			if(StringUtils.isNotBlank(equipment_bimodel_id)){
				String [] bim_ids = equipment_bimodel_id.split(",");
				return previewFileServiceImpl.getUrlByFiles(bim_ids);
			}
		}
		return "";
	}


}
